package org.openkanban.client.ui.presenter;

import java.util.ArrayList;
import java.util.List;

import org.openkanban.client.ui.board.KanbanEditView;
import org.openkanban.shared.dto.UserDto;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.SC;

/**
 * Callback that handles get kanban user request
 */
public class GetKanbanUserRequestCallback implements RequestCallback {

    KanbanEditView kanbanEditView;

    public GetKanbanUserRequestCallback(KanbanEditView kanbanEditView) {
	super();
	this.kanbanEditView = kanbanEditView;
    }

    @Override
    public void onResponseReceived(Request request, Response response) {
	if (response.getStatusCode() < 300) {
	    JSONArray kanbanUser = new JSONArray(JSON.decode(response.getText()));
	    List<UserDto> userResultList = new ArrayList<UserDto>();

	    for (int i = 0; i < kanbanUser.size(); i++) {
		UserDto user = new UserDto(kanbanUser.get(i).isObject().toString());
		userResultList.add(user);
	    }

	    kanbanEditView.showKanbanUser(userResultList);
	} else {
	    SC.say("Fehler: " + response.getStatusText());
	}

    }

    @Override
    public void onError(Request request, Throwable exception) {
    }

}
